//
//  LBColorBurnTextureFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 4/3/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBColorBurnTextureFilter.h"
#include "LBGraphics.h"

char fShaderStrColorBurnTexture[] = ""
"precision highp float;"

"varying vec2 vtexCoor;"
"varying vec4 vposition;"
"uniform sampler2D texture;"
"uniform sampler2D texture2;"
"uniform float opacity;"


// colorBurn function
"vec4 colorBurn(vec4 base, vec4 blend, float opacity){"

"   vec4 result;"
"   if(base.r + blend.r <= 1.0) result.r = 0.0;"
"   else result.r = base.r + blend.r - 1.0;"

"   if(base.g + blend.g <= 1.0) result.g = 0.0;"
"   else result.g = base.g + blend.g - 1.0;"

"   if(base.b + blend.b <= 1.0) result.b = 0.0;"
"   else result.b = base.b + blend.b - 1.0;"

"   result = result*blend.a*base.a + base*base.a*(1.0 - blend.a) + blend*blend.a*(1.0 - base.a);"
"   result.a = base.a*(1.0 - blend.a) + blend.a;"
"   result = mix(base, result, opacity);"
"   return result;"
"}"
// END colorBurn function


"void main()"
"{"
"    lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"    lowp vec4 blendColor = texture2D(texture2, vtexCoor);"
"    textureColor = colorBurn(textureColor, blendColor, opacity);"
"    gl_FragColor = textureColor;"

"}";

LBColorBurnTextureFilter::LBColorBurnTextureFilter(){
    init(0, fShaderStrColorBurnTexture);
    LBGraphics::getInstance()->useProgram(program);
    
    
    opacityUniform = program->uniformIndex("opacity");
    setOpactiy(1.0f);
}